package com.bestv.search.management.datasyn;

import com.bestv.search.common.cache.SystemCache;
import com.bestv.search.management.command.*;
import org.apache.log4j.Logger;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean;

import java.util.Date;

/**
 * Created by charle-chen on 15/12/9.
 */
public class TopKeywordSync extends QuartzJobBean {

    static boolean isRunning = false;
    Logger logger = Logger.getLogger(TopKeywordSync.class);
    private TopKeywordCommand topCommand;
    private SystemCache systemCache;

    protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
        if (!isRunning) {
            isRunning = true;
            run();
        } else {
            logger.error("Last sync process is still running");
        }
    }

    private void run() {
        try{
            logger.info("Start update topkeyword count @" + new Date());
            topCommand.updateTopKeywordCount(systemCache.getLocalDataSourceKey());
        }finally {
            logger.info("Update topkeyword count finished @" + new Date());
            isRunning = false;
        }
    }

    public TopKeywordCommand getTopCommand() {
        return topCommand;
    }

    public void setTopCommand(TopKeywordCommand topCommand) {
        this.topCommand = topCommand;
    }

    public SystemCache getSystemCache() {
        return systemCache;
    }

    public void setSystemCache(SystemCache systemCache) {
        this.systemCache = systemCache;
    }
}
